﻿using Microsoft.Build.Construction;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace xycn.framework.PublishSolution
{
    /// <summary>
    /// 解决方案树节点
    /// </summary>
    class ProjectNode
    {
        public ProjectNode parentNode = null;
        public string targetBuildDir = "";
        public SolutionProjectType projType = SolutionProjectType.SolutionFolder;
        public ProjectInSolution projInfo = null;
        public List<ProjectNode> childeNodes = new List<ProjectNode>();
    }

    /// <summary>
    /// 解决方案树
    /// </summary>
    static class SolutionTree
    {
        /// <summary>
        /// 解决方案树项目节点字典
        /// </summary>
        public static Dictionary<string, ProjectNode> projDic =
            new Dictionary<string, ProjectNode>();

        /// <summary>
        /// 解决方案树根节点
        /// </summary>
        public static ProjectNode rootNode = new ProjectNode();

        /// <summary>
        /// 创建解决方案树
        /// </summary>
        /// <param name="out_path"></param>
        /// <param name="slnfile_name"></param>
        public static void BuildSolutionTree(string out_path, string slnfile_name)
        {
            rootNode.targetBuildDir = out_path;

            //读取xycn.framework解决方案
            SolutionFile _solutionFile = SolutionFile.Parse(slnfile_name);

            //依次处理每个项目，并根据次序构建目录结构
            for (; ; )
            {
                bool do_add = false;
                foreach (ProjectInSolution proj in _solutionFile.ProjectsInOrder)
                {
                    if (projDic.ContainsKey(proj.ProjectGuid))
                        continue;

                    //无父节点
                    if (proj.ParentProjectGuid == null)
                    {
                        ProjectNode new_node = new ProjectNode()
                        {
                            parentNode = rootNode,
                            projType = proj.ProjectType,
                            projInfo = proj,
                            targetBuildDir = rootNode.targetBuildDir +
                                "\\" + proj.ProjectName
                        };

                        //添加子节点
                        rootNode.childeNodes.Add(new_node);
                        //添加索引
                        projDic.Add(proj.ProjectGuid, new_node);

                        do_add = true;
                        break; ;
                    }

                    //父节点尚未添加则跳过该节点，等待下次循环
                    if (!projDic.ContainsKey(proj.ParentProjectGuid))
                        continue;

                    //获取父节点
                    ProjectNode parent = projDic[proj.ParentProjectGuid];

                    ProjectNode node = new ProjectNode()
                    {
                        projType = proj.ProjectType,
                        projInfo = proj,
                        parentNode = parent,
                        targetBuildDir = parent.targetBuildDir +
                                "\\" + proj.ProjectName
                    };

                    //添加子节点
                    parent.childeNodes.Add(node);
                    //添加索引
                    projDic.Add(proj.ProjectGuid, node);

                    do_add = true;
                }
                if (!do_add)
                    break;
            }
        }
    }
}
